<?php
/**
 * use kernelius_cache for sessions
 * @author Fernando Palomo Garcia fernando@kernelius.com
 * @version 0.1
 * @license Apache License 2.0
 */
class Kernelius_Session
{
    /**
     * Session lifetime
     *
     * @var integer
     */
    const DEFAULT_MAX_LIFETIME=3600;


    /**
     *
     * @var Kernelius_MemcacheCluster
     */
    protected $cache;


    /**
     * //TODO: load servers from xml file
     * 
     */
    public function __construct()
    {
        $servers= array(
            array(
                "host"  =>  "127.0.0.1",
                "port"  =>  11211,
            ),
            array(
                "host"  =>  "127.0.0.1",
                "port"  =>  11212,
            )
        );
        $persistent_config= array(
            "host"          =>  "localhost",
            "user"          =>  "root",
            "password"     =>  "caro",
            "db"            =>  "sessions"
        );
        $this->cache=new Kernelius_Cache($servers,"Mysql",$persistent_config);
    }


    /**
     *
     */
    public function __destruct()
    {
        session_write_close();
    }


    /**
     *
     * @return int
     */
    public function getLifeTime()
    {
        return self::DEFAULT_MAX_LIFETIME;
    }


    /**
     *  set this class as session save handler
     * @return Kernelius_Session
     */
    public function setSaveHandler()
    {
            session_set_save_handler(
                array($this, 'open'),
                array($this, 'close'),
                array($this, 'read'),
                array($this, 'write'),
                array($this, 'destroy'),
                array($this, 'gc')
            );
        return $this;
    }

    /**
     * Open session
     *
     * @param string $savePath ignored
     * @param string $sessName ignored
     * @return boolean
     */
    public function open($savePath, $sessName)
    {
        return true;
    }


    /**
     * Close session
     *
     * @return boolean
     */
    public function close()
    {
        $this->gc($this->getLifeTime());

        return true;
    }

    /**
     * Fetch session data
     *
     * @param string $sessId
     * @return string
     */
    public function read($sessId)
    {
        $data= $this->cache->get($sessId );

        return $data;
    }

    /**
     * Update session
     *
     * @param string $sessId
     * @param string $sessData
     * @return boolean
     */
    public function write( $sessId, $sessData)
    {
        $this->cache->set( $sessId, $sessData, self::DEFAULT_MAX_LIFETIME);

        return true;
    }

    /**
     * Destroy session
     *
     * @param string $sessId
     * @return boolean
     */
    public function destroy($sessId)
    {
        $this->cache->delete($sessId);
        return true;
    }

    /**
     * Garbage collection
     *
     * @param int $sessMaxLifeTime ignored
     * @return boolean
     */
    public function gc($sessMaxLifeTime)
    {
        $this->cache->gc($sessMaxLifeTime/60);
        return true;
    }
}